package com.heart.admin.controller;

import com.heart.common.constant.AppCode;
import com.heart.common.constant.AppConstant;
import com.heart.common.utils.CollectionUtils;
import com.heart.common.utils.StringUtils;
import com.heart.dao.dto.RequestResult;
import com.heart.dao.entity.SecurityUser;
import com.heart.service.SecurityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/securityUser")
public class SecurityUserController {
@Autowired
    private SecurityUserService securityUserService;

    @PostMapping("/login")
    public RequestResult login(@RequestBody Map<String,String> map){
        Map<String, Object>  result= securityUserService.login(map.get("username"), map.get("password"));
        if (CollectionUtils.isNotEmpty(result)){
            return new RequestResult(AppCode.SUCCESS, result, AppConstant.SUCCESS);
        }
        return new RequestResult(AppCode.AUTH_ERROR, "", AppConstant.SUCCESS);
    }

    @PostMapping("/info")
    public RequestResult info(@RequestBody Map<String,String> map){
        return new RequestResult(AppCode.SUCCESS, securityUserService.info(map.get("token")), AppConstant.SUCCESS);
    }

    /**
     * 获取密码盐
     * @return
     */
    @PostMapping("/getSalt")
    public RequestResult getSalt(@RequestBody Map<String,String> map){
        return new RequestResult(AppCode.SUCCESS, securityUserService.getSalt(map.get("userCode")), AppConstant.SUCCESS);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public RequestResult logout(){
        return new RequestResult(AppCode.SUCCESS, AppConstant.SUCCESS, AppConstant.SUCCESS);
    }

    /**
     * 获取所有正常的用户
     * @return
     */
    @PostMapping("/getSecurityUserList")
    public RequestResult getSecurityUserList(){
        return new RequestResult(AppCode.SUCCESS, securityUserService.getSecurityUserList(), AppConstant.SUCCESS);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PostMapping("/updateSecurityUser")
    public RequestResult updateSecurityUser(@RequestBody SecurityUser user){
        return new RequestResult(AppCode.SUCCESS, securityUserService.updateSecurityUser(user), AppConstant.SUCCESS);
    }

    /**
     * 删除用户
     * @param user
     * @return
     */
    @PostMapping("/deleteSecurityUser")
    public RequestResult deleteSecurityUser(@RequestBody SecurityUser user){
        return new RequestResult(AppCode.SUCCESS, securityUserService.deleteSecurityUser(user), AppConstant.SUCCESS);
    }
    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/saveSecurityUser")
    public RequestResult saveSecurityUser(@RequestBody SecurityUser user){
        return new RequestResult(AppCode.SUCCESS, securityUserService.saveSecurityUser(user), AppConstant.SUCCESS);
    }
    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/updateSecurityUserPassword")
    public RequestResult updateSecurityUserPassword(@RequestBody SecurityUser user){
        return new RequestResult(AppCode.SUCCESS, securityUserService.updateSecurityUserPassword(user), AppConstant.SUCCESS);
    }

}
